package jp.sinya.temicontact.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import jp.sinya.temicontact.R;
import jp.sinya.temicontact.utils.LogUtils;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * @author Koizumi Sinya
 * @date 2018/01/12. 23:35
 * @edithor
 * @date
 */
public class MyBroadcastReceiver extends BroadcastReceiver {

    // 复写onReceive()方法
    // 接收到广播后，则自动调用该方法
    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("message");
        String id = intent.getStringExtra("id");
        LogUtils.Sinya("广播接收到信息 " + msg);
        showNotification(context, msg);
    }

    private void showNotification(Context context, String msg) {
        NotificationManager manger = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(context);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("A new message is coming");
        builder.setContentText(msg);
        builder.setAutoCancel(true);

        manger.notify(0, builder.build());
    }
}